import React, { Component } from 'react';

class UserAdd extends Component {
    handleSubmit = () => {
        let username = this.refs.username.value;
        let password = this.refs.password.value;
        let user = { username, password, id: Date.now() };
        let users = JSON.parse(localStorage.getItem('USERS') || "[]");
        users.push(user);
        localStorage.setItem('USERS', JSON.stringify(users));
        this.props.history.push('/user/list')
    }
    render() {
        return (
            <form onSubmit={this.handleSubmit}>
                <div className="form-group">
                    <label htmlFor="username" className="control-label">
                        用户名
                </label>
                    <input type="text" className="form-control" ref="username" placeholder="用户名" />
                </div>
                <div className="form-group">
                    <label htmlFor="username" className="control-label">
                        密码
                </label>
                    <input type="password" className="form-control" ref="password" placeholder="密码" />
                </div>
                <div className="form-group">

                    <input type="submit" className="btn btn-danger" />
                </div>
            </form>
        );
    }
}

export default UserAdd;